package com.google.code.stalkernet.advertisements;

import java.util.HashSet;
import java.util.Set;

import com.google.code.stalkernet.advertisement.Advertisement;

/**
 * Simple implementation of {@link Advertisements} interface.
 *
 * @author Mikhail Vladimirov
 */
public class SimpleAdvertisements extends AbstractAdvertisements
{
    private final Set <Advertisement> advertisements = new HashSet <> ();

    /**
     * {@inheritDoc}
     */
    @Override
    public void addAdvertisement (Advertisement advertisement)
    {
        if (advertisement == null)
            throw new IllegalArgumentException ("Advertisement is null");

        if (advertisements.add (advertisement))
            fireAdvertisementAdded (advertisement);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void removeAdvertisement (Advertisement advertisement)
    {
        if (advertisement == null)
            throw new IllegalArgumentException ("Advertisement is null");

        if (advertisements.remove (advertisement))
            fireAdvertisementRemoved (advertisement);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Advertisement [] getAllAdvertisements ()
    {
        return advertisements.toArray (
            new Advertisement [advertisements.size ()]);
    }
}
